package com.example.demo.service;

import com.example.demo.controller.LikeController;
import com.example.demo.controller.UserController;
import com.example.demo.entity.PostVO;
import com.example.demo.entity.User;
import com.example.demo.mapper.PostMapper;
import com.example.demo.entity.Post;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

@Service
public class PostService {
    @Autowired
    private UserController userController;
    @Autowired
    private PostMapper postMapper;
    @Autowired
    private LikeController likeController;

    // 保存帖子
    public Long savePost(Long userId, String title, String content, List<Long> tagIds) {
        Post post = new Post();
        post.setUserId(userId);
        post.setTitle(title);
        post.setContent(content);
        postMapper.insertPost(post); // 插入帖子

        // 插入帖子标签关联
        if (tagIds != null && !tagIds.isEmpty()) {
            postMapper.insertPostTags(post.getId(), tagIds);
        }

        return post.getId(); // 返回帖子 ID
    }

    // 查询帖子列表
    public List<PostVO> getPostsWithUserInfoByKeyword(
            String keyword,
            String tagName,
            String startTime,
            String endTime,
            int page,
            int pageSize,
            HttpServletRequest request) { // 添加 HttpServletRequest 参数
        int offset = (page - 1) * pageSize;
        List<PostVO> posts = postMapper.selectPostsWithUserInfoByKeyword(keyword, tagName, startTime, endTime, pageSize, offset);

        // 为每个帖子设置标签
        for (PostVO post : posts) {
            List<String> tags = postMapper.selectTagsByPostId(post.getId());
            post.setTags(tags);
        }

        // 获取当前用户信息
        Map<String, Object> currentUserResponse = userController.getCurrentUser(request);
        Long userId = null;
        if (currentUserResponse.get("code").equals(200)) {
            userId = ((User) currentUserResponse.get("user")).getId();
        }

        // 为每个帖子附加点赞信息和点赞数
        for (PostVO post : posts) {
            // 获取点赞数
            Map<String, Object> likeCountResponse = likeController.getLikeCount(post.getId());
            if (likeCountResponse.get("code").equals(200)) {
                post.setLikeCount((Integer) likeCountResponse.get("likeCount"));
            }

            // 获取当前用户是否点赞
            if (userId != null) {
                Map<String, Object> isLikedResponse = likeController.isLiked(post.getId(), request);
                if (isLikedResponse.get("code").equals(200)) {
                    post.setLiked((Boolean) isLikedResponse.get("isLiked"));
                }
            } else {
                post.setLiked(false); // 用户未登录，默认未点赞
            }
        }

        return posts;
    }

    // 查询符合条件的帖子总数
    public int countPostsByKeyword(
            String keyword,
            String tagName,
            String startTime,
            String endTime) {
        return postMapper.countPostsByKeyword(keyword, tagName, startTime, endTime);
    }

    // 关联标签到帖子
    public void addTagsToPost(Long postId, List<Long> tagIds) {
        if (tagIds != null && !tagIds.isEmpty()) {
            postMapper.insertPostTags(postId, tagIds);
        }
    }
    //根据id查询帖子
    public PostVO getPostById(Long postId) {
        // 查询帖子信息
        PostVO post = postMapper.selectPostById(postId);
        if (post != null) {
            // 查询帖子标签
            List<String> tags = postMapper.selectTagsByPostId(postId);
            post.setTags(tags);
        }
        return post;
    }

    // 根据用户ID或用户名查询帖子
    public List<PostVO> getPostsByUser(Long userId, String username, int page, int pageSize) {
        int offset = (page - 1) * pageSize;
        List<PostVO> posts = postMapper.selectPostsByUser(userId, username, pageSize, offset);

        // 为每个帖子设置标签
        for (PostVO post : posts) {
            List<String> tags = postMapper.selectTagsByPostId(post.getId());
            post.setTags(tags);
        }

        return posts;
    }

    // 根据用户ID或用户名查询符合条件的帖子总数
    public int countPostsByUser(Long userId, String username) {
        return postMapper.countPostsByUser(userId, username);
    }
}
